/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.contents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Label;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import config.Debug;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class TextLabelBox extends ContentBox {

	private String labelFormat;
	private JTextArea label;
	private JLabel jlabel;
	private boolean successfullyInitialized = false;

	public TextLabelBox(String jsonString) {
		this.labelFormat = jsonString;
		label = new JTextArea();
		label.setLineWrap(true);
		label.setWrapStyleWord(true);
		label.setEditable(false);
		label.setBackground(new Color(0, 0, 0, 0));
		label.setBorder(null);
		final String format = this.labelFormat;
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				TextLabelBox.this.setLabelProperties(format);
			}
		});
		this.setLayout(new BorderLayout());
		this.add(label);
		this.setSuccessfullyInitialized(true);
	}
	
	public TextLabelBox(String jsonString, boolean jlabel) {
		this.labelFormat = jsonString;
		this.jlabel = new JLabel("txt", JLabel.CENTER);
		final String format = this.labelFormat;
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				TextLabelBox.this.setLabelProperties(format);
			}
		});
		this.setLayout(new BorderLayout());
		this.add(this.jlabel);
		this.setSuccessfullyInitialized(true);
	}

	public void unsetWrap() {
		this.label.setLineWrap(false);
		this.label.setAlignmentX(CENTER_ALIGNMENT);
	}

	public JTextArea getLabel() {
		return label;
	}

	public TextLabelBox(String jsonString, int width, int height) {
		this(jsonString);
		this.setSize(new Dimension(width, height));
		this.label.setSize(new Dimension(width, height));
	}

	private void setLabelProperties(String jsonString) {
		JSONObject obj = (JSONObject) JSONValue.parse(jsonString);
		// System.out.println(obj);
		String text = obj.get("text") == null ? "" : obj.get("text").toString();
		String font = obj.get("font") == null ? "Arial" : obj.get("font").toString();
		String fontSize = obj.get("font-size") == null ? "14" : obj.get("font-size").toString();
		int style = obj.get("font-style") == null ? Font.PLAIN : Font.ITALIC;
		style = obj.get("font-weight") == null ? style : style | Font.BOLD;
		String fontColor = obj.get("color") == null ? "000000" : obj.get("color").toString();
		String prefix = obj.get("prefix") == null ? "" : obj.get("prefix").toString();
		String postfix = obj.get("postfix") == null ? "" : obj.get("postfix").toString();

		float textAlign = Component.LEFT_ALIGNMENT;
		if (obj.get("font-align") != null) {
			if (obj.get("font-align").toString().equalsIgnoreCase("right")) {
				textAlign = Component.RIGHT_ALIGNMENT;
			} else if (obj.get("font-align").toString().equalsIgnoreCase("center")) {
				textAlign = Component.CENTER_ALIGNMENT;
			}
		}

		// set text
		this.setLabelText(text, prefix, postfix);
		// set fonts
		this.setLabelFont(font, fontSize, style);
		// set text label color
		this.setLabelColor(fontColor);

	}

	private void setLabelFont(String fontName, String strFontSize, int fontStyle) {
		if (null != fontName) {
			int fontSize = (strFontSize != null) ? Integer.parseInt(strFontSize) : 14;
			Font font = new Font(fontName, fontStyle, fontSize);
			if(this.label!=null) {
				label.setFont(font);
			} else {
				this.jlabel.setFont(font);
			}
		}
	}

	private void setLabelText(String format, String prefix, String postfix) {
		if (null != format) {
			if(this.label!=null) {
				label.setText(prefix + format.toString() + postfix);
			} else {
				this.jlabel.setText(prefix + format.toString() + postfix);
			}
			
			
		}
	}

	private void setLabelColor(String format) {
		if (null != format) {
			Color color = Color.black;
			String colorString = format;
			if (colorString.length() == 6) {
				try {
					color = new Color(Integer.parseInt(colorString.substring(0, 2), 16), Integer.parseInt(colorString.substring(2, 4), 16), Integer.parseInt(
							colorString.substring(4, 6), 16));
				} catch (NumberFormatException ex) {
					Debug.log("illegal font-color format for text-label", Debug.ERROR_LEVEL);
					color = Color.black;
				}
			}
			if(this.label!=null) {
				label.setForeground(color);
			} else {
				this.jlabel.setForeground(color);
			}
		}
	}

	@Override
	public String getContent() {
		return this.label.getText();
	}

	@Override
	public void cleanUp() {
		this.removeAll();
		this.revalidate();
	}

	@Override
	public void reinitialize() {
	}

	@Override
	public void initialize() {
	}

	public static void main(String[] args) {
		JSONObject testObj = new JSONObject();
		JSONObject obj = new JSONObject();
		obj.put("text", "This is a TextLabel");
		obj.put("font", "Arial");
		obj.put("font-size", 14);
		obj.put("font-style", "bold");
		obj.put("color", "AA0000");
		Debug.msg(obj.toString());
		final TextLabelBox box = new TextLabelBox(obj.toString());

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame f = new JFrame("test Frame");
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.getContentPane().add(box);
				f.setPreferredSize(new Dimension(600, 400));
				f.pack();
				f.setVisible(true);
			}
		});
	}

	@Override
	public boolean isSuccessfullyInitialized() {
		return this.successfullyInitialized;
	}

	public void setSuccessfullyInitialized(boolean successfullyInitialized) {
		this.successfullyInitialized = successfullyInitialized;
	}

}
